#!/bin/sh
USER=
a=0
fin=./use.txt
fxn=`cat ./use.txt`
#cat 查看文件
for u in $fxn
do
	USER[$a]=$u
	a=$(($a+1))
done
# 将文本信息输入到数组
function add_user {
    while true
    do
           echo -n "请输入学员姓名："
            read xm
           echo -n "请输入学员性别："
            read xb
           echo -n "请输入学员年龄："
            read nn
             s="$xm:$xb:$nn"
   	     USER[$a]=$s
	     a=$(($a+1))
                echo "共有${#USER[@]}个"
	        echo -n "是否继续输入（y\n）:"
	           read w
		     if [ $w == "n" -o $w == "N" ];then
		       break
		     fi
     done	

 }
function query_user(){
	echo -n "请输入需要查询的学员姓名："
	  read name
  	     for i in ${USER[@]}
	     do
	         name1=`echo ${i} | awk -F":" '{print $1}'`
	            if [ $name1 == "$name" ];then
		        echo ${i}
		        break;
	            fi
	     done
}
function update_user {
	echo -n "请输入需要修改的学员姓名："
	   read name
	   for j in ${USER[@]}
	   do
	       name2=`echo ${j} | awk -F":" '{print $1}'`
	          if [ $name2 == "$name" ];then
		     echo -e "请输入需要更改学员信息（姓名：性别：年龄）："
		     read name3
		     USER[$j1]="$name3";
	          else
		     j1=$(($j1+1))
	          fi
           done 
 
 }
 
while true;
do
     echo "1. 添加学员"
     echo "2. 查询学员"
     echo "3. 更新学员"
     echo "4. 退出系统"
 
    echo -n "请选择:"
     read choice i
     if [ $choice -eq 1 ]; then
         add_user
     elif [ $choice -eq 2 ]; then
         query_user
     elif [ $choice -eq 3 ]; then
         update_user
     elif [ $choice -eq 4 ]; then
	echo "是否保存信息(y\n):"
	read c
	    if [ $c == "y" -o $c == "Y" ];then
		cat /dev/null > $fin
		for ((o=0;$o<$a;o++))
		do
		    echo ${USER[$o]} >> $fin
		done
                exit 0
	    elif [ $c == "n" -o $c == "N" ];then
	 	exit
     else
         echo "错误的选择!!!"
            fi
    fi
 done
            
